package com.zy.yun.provider.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zy.yun.base.entity.RoleOrganization;
import com.zy.yun.base.entity.RoleRes;
import com.zy.yun.provider.dao.RoleResMapper;
import com.zy.yun.provider.service.IRoleOrganizationService;
import com.zy.yun.provider.service.IRoleResService;
import com.zy.yun.utils.TableCacheUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色资源关系表 服务实现类
 * </p>
 *
 * @author 王彦委
 * @since 2019-10-20
 */
@Service
public class RoleResServiceImpl extends ServiceImpl<RoleResMapper, RoleRes> implements IRoleResService {


    @Autowired
    private IRoleResService roleResService;

    @Autowired
    private TableCacheUtil tableCacheUtil;

    @Autowired
    private IRoleOrganizationService roleOrganizationService;

    /**
     * @Author: 王彦委
     * @Description: 保存角色资源权限
     * @Date: 22:32 2019/10/20
     */
    @Transactional
    @Override
    public boolean saveAuth(Long roleId, List<Long> resIds) {

        //1 清空本角色对应的资源
        EntityWrapper<RoleRes> roleResQueryWrapper = new EntityWrapper<>();
        roleResQueryWrapper.eq("roleId", roleId);
        roleResService.delete(roleResQueryWrapper);

        //2 判断 resIds是否为空
        if (CollectionUtils.isEmpty(resIds)) {
            return true;
        }

        //3 非空增加
        ArrayList<RoleRes> roleRess = new ArrayList<>();

        RoleRes rr = null;
        for (Long id : resIds) {
            rr = new RoleRes();
            rr.setResId(id);
            rr.setRoleId(roleId);
            roleRess.add(rr);
        }

        if (roleResService.insertBatch(roleRess)) {

            //更新一下 redis中表缓存
            tableCacheUtil.updateTableSysRoleRes();
            return true;
        }
        return false;
    }


    /**
     * @Author: 王彦委
     * @Description: 保存角色数据权限
     * @Date: 10:49 2019/11/10
     */
    @Transactional
    @Override
    public boolean saveDataAuth(Long roleId, List<Long> organizationIds) {

        //1 清空本角色对应的资源
        EntityWrapper<RoleOrganization> roleOrganizationQueryWrapper = new EntityWrapper<>();
        roleOrganizationQueryWrapper.eq("roleId", roleId);
        roleOrganizationService.delete(roleOrganizationQueryWrapper);

        //2 判断 resIds是否为空
        if (CollectionUtils.isEmpty(organizationIds)) {
            return true;
        }

        //3 非空增加
        ArrayList<RoleOrganization> roleOrganizations = new ArrayList<>();

        RoleOrganization rr = null;
        for (Long id : organizationIds) {
            rr = new RoleOrganization();
            rr.setOrganizationId(id);
            rr.setRoleId(roleId);
            roleOrganizations.add(rr);
        }

        if (roleOrganizationService.insertBatch(roleOrganizations)) {

            //更新一下 redis中表缓存
            tableCacheUtil.updateTableSysRoleOrganization();
            return true;
        }
        return false;
    }
}
